#include <gtest/gtest.h>
#include <loop_module/loop.h>
#include <string>
#include <tls_socket_connection.h>

TEST(Test, Hello)
{
    printf("Hello.\n");
}

class TlsSocketObserver : public SocketObserver
{
public:
    TlsSocketObserver() = default;
    ~TlsSocketObserver() override = default;
    void Update(SocketCommand * command) override;
};

void TlsSocketObserver::Update(SocketCommand * command)
{
    switch (command->Cmd())
    {
        case SocketCommand::EmCmdConnect:
        {
            printf("connected.\n");
            //std::string message = "GET / HTTP/1.1\r\nHost: www.bing.com\r\nAccept: *\r\n\r\n";
            //command->Connection()->AsyncWrite((const uint8_t*)message.c_str(), message.length());
            break;
        }
        case SocketCommand::EmCmdDisconnect:
        {
            printf("disconnected.\n");
            break;
        }
        case SocketCommand::EmCmdError:
        {
            printf("error.\n");
            break;
        }
        case SocketCommand::EmCmdRead:
        {
            printf("message:%s.\n", (char*)(command->Data()));
            command->Connection()->Close();
            break;
        }
        default:
            break;
    }
}

TEST(Test, Connect)
{
    Loop                       loop;
    auto                       connection = std::make_shared<TlsSocketConnection>(&loop);
    std::string                domain     = "www.bing.com";
    std::string                protocol   = "https";
    std::vector<SocketAddress> socket_address;
    SocketAddress::Address(domain, protocol, socket_address);
    if (socket_address.empty())
    {
        ASSERT_EQ(true, false);
    }
    SocketAddress address = *socket_address.data();
    connection->RegisterSocketObserver(std::make_shared<TlsSocketObserver>());
    connection->OpenSocket(address, {});
    connection->AsyncConnect(address, SocketConnection::Option{.m_host_name = "www.baidu.com",.m_enable_nonblock = 1});
    loop.Run();
}
